package de.stefanocke.roo.wicket.persistence;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.springframework.roo.classpath.details.MethodMetadata;
import org.springframework.roo.classpath.persistence.PersistenceMemberLocator;
import org.springframework.roo.model.JavaType;

import de.stefanocke.roo.wicket.model.IPropertyFilter;

/**
 * Filter, that excludes ID and version properties of an entity.
 * 
 * @author stefan
 *
 */
@Component
@Service
public class PersistencePropertyFilter implements IPropertyFilter {

	@Reference
	private PersistenceMemberLocator persistenceMemberLocator;
	
	@Override
	public boolean isGUIProperty(String clientMID, JavaType modelClass, MethodMetadata accessor) {
		//TODO: Register dependencies in case the ID or version accessor changes?
		MethodMetadata identifierAccessor = persistenceMemberLocator.getIdentifierAccessor(modelClass);
		MethodMetadata versionAccessor = persistenceMemberLocator.getVersionAccessor(modelClass);
		if(accessor.hasSameName(identifierAccessor, versionAccessor)){
			return false;
		}
		return true;
	}

}
